// import { InjectRepository } from '@nestjs/typeorm';
// import { User } from './user.entity';
// import { Repository } from 'typeorm';
import { Inject } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { UserMongooseRepository } from './repository/user.mongoose.repository';
import { UserTypeormRepository } from './repository/user.typeorm.repository';
import { UserPrismaRepository } from './repository/user.prisma.repository';
// import { UserAbstractRepository } from './user-abstract.repository';
import { UserAdapter } from './user.interface';
import * as argon2 from 'argon2';
export class UserRepository implements UserAdapter {
  constructor(
    @Inject(REQUEST) private request: Request,
    private userMongooseRepository: UserMongooseRepository,
    private userTypeormRepository: UserTypeormRepository,
    private userPrismaRepository: UserPrismaRepository,
  ) {}
  findAll(page?: number, limit?: number): Promise<any[]> {
    const client = this.getRepositort();
    return client.findAll(page, limit);
  }
  findOne(username: string): Promise<any> {
    const client = this.getRepositort();
    return client.findOne(username);
  }
  async create(userObj: any): Promise<any> {
    const client = this.getRepositort();
    const { password } = userObj;
    const newHashPass = await argon2.hash(password);
    userObj = { ...userObj, password: newHashPass };
    return client.create(userObj);
  }
  update(userObj: any): Promise<any> {
    const client = this.getRepositort();
    return client.update(userObj);
  }
  delete(id: number): Promise<any> {
    const client = this.getRepositort();
    return client.delete(id);
  }
  getRepositort(): UserAdapter {
    const headers = this.request.headers;
    const tenantId = headers['x-tenant-id'] || 'default';
    if (tenantId === 'mongo' || tenantId === 'mongo1') {
      return this.userMongooseRepository;
    } else if (
      tenantId === 'typeorm1' ||
      tenantId === 'typeorm2' ||
      tenantId === 'typeorm3'
    ) {
      return this.userTypeormRepository;
    } else if (tenantId === 'prisma1' || tenantId === 'prisma2') {
      return this.userPrismaRepository;
    }
    console.log(1);
    return this.userPrismaRepository;
  }
}
